import axios from 'axios'

// 设置baseURL
axios.defaults.baseURL = 'http://127.0.0.1:8080/backstage'

// 创建axios对象，暴露
export default axios

axios.interceptors.request.use(function (config) {
  // Do something before request is sent
  if (['/user/login', '/user/register'].indexOf(config.url) === -1) {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = "Bearer " + token;
    }else{
      if (process.client) {
        window.location.href="/login/login";
      }
    }
  }
  return config;
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

axios.interceptors.response.use(function (response) {
  // Do something with response data
  let that = this;
  console.log(response);
  if(response.data.code!=1000){
    if(response.data.code!=203){
      if (process.client) {
        window.location.href="/login/login";
      }
    }
  }
  return response;
}, function (error) {
  // Do something with response error
  return Promise.reject(error);
});

